<?php
/**
 * /library/mvc-helper/Form/Abstract.php
 * @author qqinxl
 * 2008/03/22
 * 
 * @category  
 * @package  
 * @copyright  Copyright (c) {}
 */
class Form_Abstract extends Zend_Form {
	protected $_from_legend;
	
	protected $_i18nErrors;
	
	protected $_iniFile = null;
	
	protected $_isChangeFormat = true;
	
	public function __construct() {
		//$this->addElementPrefixPath('Form_Element', 'Form/Element/');
		
		if (empty ( $this->_iniFile )) {
			$classname = get_class ( $this );
			$classname = explode ( "_", $classname );
			$filename = '';
			$path = '';
			$count = count ( $classname );
			if ($count == 1) {
				throw new Exception ( "The name of the class form has some mistakes." );
			}
			foreach ( $classname as $i => $value ) {
				if ($i != 0) {
					$filename .= strtolower ( $value ) . '.';
				}
				if ($i != $count - 1) {
					$path .= $value . DIRECTORY_SEPARATOR;
				}
			}
			$this->_from_legend = 'from.'.$filename.'legend';
			$filename = $path . $filename . 'ini';
			
			$config = new Zend_Config_Ini ( MODELS_MVC_DIR . DIRECTORY_SEPARATOR . $filename );
		} else {
			$form_dir = MODELS_MVC_DIR . DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR;
			$config = new Zend_Config_Ini ( $form_dir . $this->_iniFile);			
		}
		parent::__construct ( $config );
		$this->_translator = self::getDefaultTranslator ();
		$this->init ();
	}
	public function getFormLegend(){
		return $this->_from_legend;
	}
	public function init() {
		if ($this->_isChangeFormat) {
			$this->changeFormat ();
		}
	}
	protected function _($string, $options = null) {
		return $this->getText ( $string, $options );
	}
	protected function getText($string, $options = null) {
		$msg = $this->_translator->_ ( $string );
		if (! empty ( $options ) and is_array ( $options )) {
			foreach ( $options as $key => $value ) {
				$msg = str_replace ( '{' . $key . '}', $value, $msg );
			}
		}
		return $msg;
	}
	
	public function translateError() {
		$this->_i18nErrors = null;
		$codes = $this->getErrors ();
		foreach ( $codes as $key => $errors ) {
			if (empty ( $errors )) {
				continue;
			}
			foreach ( $errors as $error ) {
				$error_i18n = 'error.' . $key . '.' . $error;
				$msg = $this->_ ( $error_i18n, array ('value' => $this->getElement ( $key )->getValue () ) );
				$this->_i18nErrors [$key] = $msg;
			}
		}
		$this->_errorsExist = false;
		return $this->_i18nErrors;
	}
	
	public function getI18nErrors() {
		return $this->_i18nErrors;
	}
	
	protected function changeFormat() {
		$this->clearDecorators ();
		$this->addDecorator ( 'FormElements' )
			 ->addDecorator ( 'HtmlTag', array ('tag' => '<fieldset>' ) )
			 ->addDecorator ( 'Form' );
		
		$this->setElementDecorators ( 
			array (
				array ('ViewHelper' ), 
				array ('Errors' ), 
				array ('Description' ), 
				array ('Label', array ('class' => '' )), 
				array ('HtmlTag', array ('tag' => 'p' ) ) 
			) 
			);
		
		foreach ( $this->getElements () as $value ) {
			if ($value instanceof Zend_Form_Element_Submit 
				or $value instanceof Zend_Form_Element_Reset 
				or $value instanceof Zend_Form_Element_Button) {
				$value->setDecorators ( 
						array (
							array ('ViewHelper' ), 
							array ('Description' ), 
							array ('HtmlTag', array ('tag' => 'p' ,'style' =>'margin-left:130px')
						 ) ) 
					 );
			}
		}
	}
	
	
}